package com.software.loginregister.adapter;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.software.loginregister.R;
import com.software.loginregister.entity.Works;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
public class WorksDraftAdapter extends RecyclerView.Adapter<WorksDraftAdapter.ViewHolder> {
    private List<Works> draftList;
    private OnItemClickListener listener;

    public WorksDraftAdapter(List<Works> draftList) {
        this.draftList = draftList;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_works_draft, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Works draft = draftList.get(position);
        holder.tvTitle.setText(draft.getWorksName());
        // 如果没有updateTime字段，可以暂时显示空字符串或当前时间
        holder.tvUpdateTime.setText("");  // 或者使用当前时间

        if (!TextUtils.isEmpty(draft.getPhotoUrl())) {
            Glide.with(holder.itemView.getContext())
                    .load(draft.getPhotoUrl())
                    .into(holder.ivPhoto);
        }

        holder.itemView.setOnClickListener(v -> {
            if (listener != null) {
                listener.onItemClick(draft, position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return draftList.size();
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.listener = listener;
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView ivPhoto;
        TextView tvTitle;
        TextView tvUpdateTime;

        ViewHolder(View view) {
            super(view);
            ivPhoto = view.findViewById(R.id.iv_draft_photo);
            tvTitle = view.findViewById(R.id.tv_draft_title);
            tvUpdateTime = view.findViewById(R.id.tv_draft_update_time);
        }
    }

    public interface OnItemClickListener {
        void onItemClick(Works draft, int position);
    }
}